package com.example.ecas.controller;

import com.example.ecas.common.CommonResponse;
import com.example.ecas.persistence.AccountMapper;
import com.example.ecas.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    AccountMapper accountMapper;

    // 从session中获取用户数据，返回指定用户id
    @GetMapping("/getUsername")
    @ResponseBody
    public CommonResponse<String> getUsername(HttpSession session) {
        // 获取已登录用户的用户名
        Account account = (Account) session.getAttribute("account");
        String username = account.getId();
        if (username == null) {
            return CommonResponse.creatForError("暂无数据");
        } else {
            return CommonResponse.creatForSuccess(username);
        }
    }

    // 从session中获取用户数据，返回指定用户邮箱
    @GetMapping("/getEmail")
    @ResponseBody
    public CommonResponse<Object> getEmail(HttpSession session) {
        // 获取已登录用户的邮箱
        Account account = (Account) session.getAttribute("account");
        System.out.println(account);
        String email = account.getEmail();
        if (email == null) {
            return CommonResponse.creatForError("暂无数据");
        } else {
            return CommonResponse.creatForSuccess(email);
        }
    }

    // 从session中获取用户数据，返回指定用户手机号
    @GetMapping("/getPhone")
    @ResponseBody
    public CommonResponse<Object> getPhone(HttpSession session) {
        // 获取已登录用户的手机号
        Account account = (Account) session.getAttribute("account");
        System.out.println(account);
        String phone = account.getPhone();
        if (phone == null) {
            return CommonResponse.creatForError("暂无数据");
        } else {
            return CommonResponse.creatForSuccess(phone);
        }
    }

    // 从session中获取用户数据，返回指定用户权限
    @GetMapping("/getRole")
    @ResponseBody
    public CommonResponse<Object> getRole(HttpSession session){
        // 获取已登录用户的权限
        Account account = (Account) session.getAttribute("account");
        System.out.println(account);
        Boolean isAdmin = account.isAdmin();
            if (isAdmin == null) {
                return CommonResponse.creatForError("暂无数据");
            } else {
                return CommonResponse.creatForSuccess(isAdmin);
            }
    }
}
